<template>
	<view>
		<template v-if="item.role == config.ROLE_AI">
			<view v-if="item.status == 'completed' || item.status == 1">
				<chat-ai-markdown :markdown="item.content" :aiMode="true"></chat-ai-markdown>
			</view>
			<view v-else>
				<u-loading></u-loading>
			</view>
		</template>
		<template v-if="item.role == config.ROLE_MY">
			<view class="chat-my">
				<view class="chat-my-content">
					<text
						v-if="item.messageType == config.MESSAGE_TYPE_TEXT || item.messageType == config.MESSAGE_TYPE_WORD">{{item.content}}</text>
					<image v-if="item.messageType == config.MESSAGE_TYPE_IMAGE" :src="item.content" mode="aspectFill"
						@click="handlePreviewImage(item.content)"></image>
					<template v-if="item.messageType == config.MESSAGE_TYPE_AUDIO">
						<u-loading v-if="item.status == 'loading'">{{item.content}}</u-loading>
						<text v-if="item.status != 'loading'">{{item.content}}</text>
					</template>
				</view>
			</view>
		</template>
		<view v-if="item.status == 'completed' || item.status == 1" class="tools"
			:style="{'justify-content': item.role == config.ROLE_MY ? 'flex-end' : '','margin-top': item.role == config.ROLE_MY ? '10rpx' : ''}">
			<!-- <view v-if="item.messageType == config.MESSAGE_TYPE_TEXT && checkedType == 2" class="tools-item" @click="_playAudio">
				<u-icon v-if="!isPlay" size="35" color="#FD2F55" name="play-circle-fill"></u-icon>
				<u-icon v-if="isPlay" size="35" color="#FD2F55" name="pause-circle-fill"></u-icon>
			</view> -->
			<view  v-if="item.messageType == config.MESSAGE_TYPE_TEXT&&item.role==2" class="tools-item1" @click="_playAudio">
				<image mode="widthFix" src="/static/aiyuyin.png" class="flex image width56 mr15"></image>
				<!-- <u-icon v-if="!isPlay" size="35" color="#FD2F55" name="play-circle-fill"></u-icon>
				<u-icon v-if="isPlay" size="35" color="#FD2F55" name="pause-circle-fill"></u-icon> -->
			</view>
			<view v-if="item.role==config.ROLE_AI" class="tools-item" @click="_regenerate">
				<text>重新生成</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		computed,
		onMounted,
		ref
	} from 'vue';
	import config from "@/config/index.js"

	const props = defineProps({
		item: {
			type: Object,
			default: () => {}
		},
		isPlay: {
			type: Boolean,
			default: false
		},
		checkedType: {
			type: Boolean,
			default: -1
		}
	})
	const emits = defineEmits(['regenerate', 'play', 'update:checkedType'])

	const _regenerate = () => {
		emits('regenerate')
	}

	const _playAudio = () => {
		emits('play')
	}

	const handlePreviewImage = (url) => {
		uni.previewImage({
			urls: [url],
			fail: (err) => {
				uni.showToast({duration: 2000,
					title: err.errMsg || "预览失败",
					icon: "none"
				})
			}
		})
	}

	onMounted(() => {

	})
</script>

<style lang="scss" scoped>
	.tools {
		display: flex;
		flex-direction: row;
		align-items: center;
		.tools-item {
			padding: 5rpx 15rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			background: rgba(255, 46, 83, 0.1);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-right: 10rpx;

			text {
				font-weight: 400;
				font-size: 24rpx;
				color: #FD2F55;
				line-height: 34rpx;
			}
		}
	}

	.chat-my {
		display: flex;
		justify-content: flex-end;

		.chat-my-content {
			max-width: 600rpx;
			padding: 20rpx;
			background: rgba(253, 47, 85, 0.1);
			border-radius: 20rpx 0rpx 20rpx 20rpx;
			word-break: break-all;
			/* 数字/字母任意位置截断换行 */
			white-space: pre-wrap;
			/* 保留换行符，允许自动换行 */

			image {
				width: 500rpx;
				height: 500rpx;
			}
		}
	}
</style>